//
//  SDHSportsNewsDetailVC.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/14.
//

#import "SDHSportsNewsDetailVC.h"
#import "SDHSportsNewsModal.h"
#import "SDHSportsNetRequest.h"
#import "SDHSportsRequest.h"
#import "SDHSportsDataManager.h"
#import <WebKit/WebKit.h>

@interface SDHSportsNewsDetailVC ()

@property (nonatomic, strong) WKWebView *webView;

@end

@implementation SDHSportsNewsDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = _model.en_title;
    [self initUI];
    [self loadData];
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"Follow" style:UIBarButtonItemStylePlain target:self action:@selector(rightClick:)];
    self.navigationItem.rightBarButtonItem = right;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)rightClick:(UIBarButtonItem *)item {
    [[SDHSportsDataManager sharedManager] addFollowNews:_model];
    [MBProgressHUD showSuccess:@"Follow succeed!"];
}

- (void)initUI {
    WKWebView *web = [[WKWebView alloc] init];
    [self.view addSubview:web];
    self.webView = web;
    
    [web mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsZero);
    }];
}

- (void)parseData:(NSDictionary *)dic {
    NSString *contentStr = [dic objectForKey:@"data"];
    
    [_webView loadHTMLString:contentStr baseURL:nil];
}

- (void)endLoading {
    [MBProgressHUD hideHUD];
}

- (void)loadData {
    SDHSportsRequest *req = [[SDHSportsRequest alloc] init];
    req.requestUrl = @"http://jsuywu8.com/guojitiyu/Tiyu1/getNewsDetails";
    req.requestPara = @{@"id" : @"18582"};
    
    [MBProgressHUD showLoading];
    __weak typeof(self) weakSelf = self;
    [[SDHSportsNetRequest sharedRequest] sendRequest:req returnValueBlock:^(id  _Nonnull obj) {
        [weakSelf parseData:obj];
        
        [weakSelf endLoading];
    } errorBlock:^(id  _Nonnull obj) {
        [weakSelf endLoading];
    } failureBlock:^{
        [weakSelf endLoading];
    }];
}

@end
